package com.atguigu.day03;

import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Flink09_Transform_Process {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //TODO 3.将数据转为Tuple2元组 使用Process实现flatMap功能
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOneDStream = streamSource.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
            /**
             *
             * @param value 传进来的数据
             * @param ctx 上下文对象
             * @param out 收集器 用于把数据发送至下游
             * @throws Exception
             */
            @Override
            public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1));
                }
            }
        });

        //4.将相同单词的数据聚合到一块
        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = wordToOneDStream.keyBy(0);

        //TODO 5.在KeyBy之后使用Process  用Process实现sum的功能
        keyedStream.process(new KeyedProcessFunction<Tuple, Tuple2<String, Integer>, Tuple2<String, Integer>>() {

            //定义一个变量，用来保存上一次计算的结果  key:单词 value：单词个数
//            private Long lastSum = 0L;
            private HashMap<String, Integer> lastSumMap = new HashMap<>();


            /**
             * 来一条数据调用一次这个方法
             *
             * @param value 传进来的数据
             * @param ctx   上下文对象
             * @param out   收集器 用于把数据发送至下游
             * @throws Exception
             */
            @Override
            public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                //当数据过来之后要判断在map集合中是否保存的有当前单词计算后的结果，如果有的话取出来，如果没有的话不要取否则可能会报空指针异常，因为取出的是null，那么就将自己保存进去
                if (lastSumMap.containsKey(value.f0)){
                    //map集合中有当前单词的计算结果
                    //取出上次计算的结果
                    Integer lastSum = lastSumMap.get(value.f0);
                    //用上一次的计算结果和当前结果做累加
                    int newSum = lastSum + value.f1;
                    //将当前计算后的结果更新到Map中，用于下一次数据来的时候做计算
                    lastSumMap.put(value.f0, newSum);
                    //将计算后的结果返回出去
                    out.collect(Tuple2.of(value.f0,newSum));
                }else {
                    //map集合中没有当前单词计算的结果，这个数据是分组中第一条数据
                    lastSumMap.put(value.f0, value.f1);
                    //out.collect(Tuple2.of(value.f0,value.f1));
                    out.collect(value);
                }

            }
        }).print();

        env.execute();

    }
}
